﻿using System;
using System.Threading;

namespace XamlMvp
{
    public class TimerService : IDisposable
    {
        readonly Timer timer;
        public event EventHandler Tick;

        public TimerService(int dueTime, int interval)
        {
            timer = new Timer(OnTimerCallback, null, dueTime, interval);             
        }

        ~TimerService()
        {
            Dispose(false);
        }

        void OnTimerCallback(object state)
        {
            if (Tick != null)
            {
                Tick(this, EventArgs.Empty);
            }
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                timer.Dispose();
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
